package com.aaxis.microservice.training.demo1.controller;

import com.aaxis.microservice.training.demo1.domain.User;
import com.aaxis.microservice.training.demo1.service.UserService;
import com.aaxis.microservice.training.demo1.util.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class UserController {

    @Autowired
    private UserService pUserService;


    Logger logger = LoggerFactory.getLogger(getClass());


    @RequestMapping("/doLogin")
    public String login(HttpServletRequest request, @ModelAttribute @Valid User pUser, BindingResult result){
        logger.info("{0}开始登陆", pUser.getUsername());
        if(result.hasErrors()) {
            List<String> errors = result.getAllErrors().stream().map(e->
                    e.getDefaultMessage()
            ).collect(Collectors.toList());
            request.setAttribute("errorMessage", errors);
            logger.info("登陆失败验证不通过,{}",errors);
            return "forward:/login";
        }
        User user = ((RestUserController) SpringUtil.getBean("restUserController")).login(pUser);
        if(user == null){
            request.setAttribute("errorMessage", "Login error");
            logger.info("登陆失败,没有找到user");
            return "forward:/login";
        }
        request.getSession().setAttribute("user", user);
        logger.info("{0}登陆成功", pUser.getUsername());
        return "redirect:/index";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){

        request.getSession().removeAttribute("user");

        return "redirect:/login";
    }

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/regist")
    public String regist(){
        return "regist";
    }

    @PostMapping("/doRegist")
    public String doRegist(HttpServletRequest request, @ModelAttribute  @Valid User user, BindingResult result){
        // validation, TODO
        if(result.hasErrors()) {
            List<String> errors = result.getAllErrors().stream().map(e->
                    e.getDefaultMessage()
            ).collect(Collectors.toList());
            request.setAttribute("errorMessage", errors);
            return "forward:/regist";
        }
        try{
            User u = ((RestUserController) SpringUtil.getBean("restUserController")).doRegist(user);
        } catch (Exception e){
            e.printStackTrace();
            request.setAttribute("errorMessage", e.getMessage());
            return "forward:/regist";
        }
        request.getSession().setAttribute("user", user);
        return "redirect:/index";
    }
}
